package academicSupSession;

import java.rmi.RemoteException;
import java.util.Collection;

import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import query.Query;
import query.QueryHome;
import student.Student;
import academicSup.AcademicSup;
import academicSup.AcademicSupHome;
import degree.Degree;
import department.Department;
import descriptor.AcademicSupDescriptor;
import descriptor.DegreeListDescriptor;
import descriptor.QueryListDescriptor;
import descriptor.StudentListDescriptor;

public class AcademicSupSessionBean implements SessionBean {
	public InitialContext initialContext;
	public AcademicSupHome academicSupHome;
	public AcademicSup academicSup;
	
	public int getId() throws RemoteException{
		return academicSup.getId();
	}
	public void setId(int id) throws RemoteException{
		academicSup.setId(id);
	}
	
	public String getLogin() throws RemoteException{
		return academicSup.getLogin();
	}
	public void setLogin(String login) throws RemoteException{
		academicSup.setLogin(login);
	}
	
	public String getPassword() throws RemoteException{
		return academicSup.getPassword();
	}
	public void setPassword(String password) throws RemoteException{
		academicSup.setPassword(password);
	}
	
	public String getFirstname() throws RemoteException{
		return academicSup.getFirstname();
	}
	public void setFirstname(String firstname) throws RemoteException{
		academicSup.setFirstname(firstname);
	}
	
	public String getSurname() throws RemoteException{
		return academicSup.getSurname();
	}
	public void setSurname(String surname) throws RemoteException{
		academicSup.setSurname(surname);
	}
	
	public String getEmail() throws RemoteException{
		return academicSup.getEmail();
	}
	public void setEmail(String email) throws RemoteException{
		academicSup.setEmail(email);
	}
	
	public Department getDepartment()
	{
		return academicSup.getDepartment();
	}
	
	public void setDepartment(Department dep)
	{
		academicSup.setDepartment(dep);
	}

	public AcademicSupDescriptor getDescriptor(){
		return new AcademicSupDescriptor(academicSup.getId(),academicSup.getFirstname(),academicSup.getSurname(),academicSup.getLogin(),academicSup.getPassword(), academicSup.getEmail(), null, null,academicSup.getDepartment().getName(),academicSup.getDepartment().getId());
	}
	public StudentListDescriptor getStudents(){
		Collection<Student> coll = academicSup.getStudents();
		StudentListDescriptor listStudent = new StudentListDescriptor();
		for (Object student : coll){
			listStudent.addStudent(((Student)student).getDescriptor());
		}
		return listStudent;
	}
	
	public QueryListDescriptor getQueries(){
		Collection<Query> coll = academicSup.getQueries();
		QueryListDescriptor listQuery = new QueryListDescriptor();
		for (Object query : coll){
			listQuery.addQuery(((Query)query).getDescriptor());
		}
		return listQuery;
	}
	
	
	public DegreeListDescriptor getDegrees(){
		Collection<Degree> coll = academicSup.getDegrees();
		DegreeListDescriptor listDegree = new DegreeListDescriptor();
		for (Object query : coll){
			listDegree.addDegree(((Degree)query).getDescriptor());
		}
		return listDegree;
	}

	public void acceptQuery(int queryId){
		Query query = null;
		QueryHome queryHome;
		try {
			queryHome = (QueryHome)initialContext.lookup("MyQuery_L");
			query = queryHome.findByPrimaryKey(queryId);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (FinderException e) {
			e.printStackTrace();
		}
		
		academicSup.acceptQuery(query);
	}
	public void rejectQuery(int queryId){
		Query query = null;
		QueryHome queryHome;
		try {
			queryHome = (QueryHome)initialContext.lookup("MyQuery_L");
			query = queryHome.findByPrimaryKey(queryId);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (FinderException e) {
			e.printStackTrace();
		}
		academicSup.rejectQuery(query);
	}
	public void ejbActivate() throws EJBException, RemoteException {
		
	}

	public void ejbPassivate() throws EJBException, RemoteException {

		
	}

	public void ejbRemove() throws EJBException, RemoteException {

		
	}

	public void setSessionContext(SessionContext arg0) throws EJBException, RemoteException {

		
	}
	
	public String ejbCreate(int id){
		
		try {
			initialContext = new InitialContext();
			academicSupHome = (AcademicSupHome)initialContext.lookup("MyAcademicSup_L");
			academicSup = academicSupHome.findByPrimaryKey(id);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (FinderException e) {
			e.printStackTrace();
		}
		return null;
	}
	public void ejbPostCreate(int id){
		
	}

}
